package com.zhenyi.youqu.core;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbPro;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.youqu.common.model.YqItem;

public class YQUtils {

	/**
	 * 更新随机有趣的索引
	 */
	public void updateItemIndex() {
		final Cache cache = Redis.use();
		final DbPro db = Db.use("youqu");

		Kv kv = Kv.by("ordered", 1);
		SqlPara sqlPara = db.getSqlPara("YQFindItems", kv);
		List<YqItem> items = YqItem.dao.find(sqlPara);
		ArrayList<Long> list = new ArrayList<>();
		for (YqItem yqItem : items) {
			list.add(yqItem.getId());
		}
		Object[] array = (Long[]) list.toArray(new Long[items.size()]);
		String key = YQConfig.KEY_ITEM_INDEX;
		cache.sadd(key, array);
	}
}
